package cn.tedu.blackhorse.service.impl;

import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.KnewProblemMapper;
import cn.tedu.blackhorse.mapper.StudentProblemMapper;
import cn.tedu.blackhorse.pojo.dto.KnewProblemAddNewDTO;
import cn.tedu.blackhorse.pojo.entity.Course;
import cn.tedu.blackhorse.pojo.entity.KnewProblem;
import cn.tedu.blackhorse.pojo.vo.KnewProblemListItemVO;
import cn.tedu.blackhorse.pojo.vo.KnewProblemStandardVO;
import cn.tedu.blackhorse.pojo.vo.StudentProblemStandardVO;
import cn.tedu.blackhorse.service.IKnewProblemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class KnewProblemServiceImpl implements IKnewProblemService {
    @Autowired
    private KnewProblemMapper knewProblemMapper;
    @Autowired
    private StudentProblemMapper studentProblemMapper;

    @Override
    public void insert(KnewProblemAddNewDTO knewProblemAddNewDTO) {
        StudentProblemStandardVO problemStandardVO = studentProblemMapper.getById(knewProblemAddNewDTO.getProblemId());
        if (problemStandardVO==null){
            String message = "添加失败,问题不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        KnewProblemStandardVO knewProblemStandardVO = knewProblemMapper.getById(knewProblemAddNewDTO.getTeacherId());
        if (knewProblemStandardVO==null){
            String message = "添加失败,回答问题的老师不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        KnewProblem knewProblem = new KnewProblem();
        knewProblem.setProblemId(knewProblemAddNewDTO.getProblemId());
        knewProblem.setProblemId(knewProblemAddNewDTO.getTeacherId());
        int row = knewProblemMapper.insert(knewProblem);
        if (row != 1){
            String message = "添加失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);

        }
    }

    @Override
    public List<KnewProblemListItemVO> list() {
        return knewProblemMapper.list();
    }





}
